<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText">
    </el-input>

    <el-tree class="filter-tree" :data="data" :props="defaultProps" default-expand-all :filter-node-method="filterNode" ref="tree">
    </el-tree>
  </div>
</template>
<script>
export default {
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    },
  },

  methods: {
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
  },

  data() {
    return {
      filterText: '',
      defaultProps: {
        children: 'children',
        label: 'name',
        key:'id'
      },
      data: [
        {
          id: 1,
          name: '一级 1',
          children: [
            {
              id: 4,
              name: '二级 1-1',
              children: [
                {
                  id: 9,
                  name: '三级 1-1-1',
                },
                {
                  id: 10,
                  name: '三级 1-1-2',
                },
              ],
            },
          ],
        },
        {
          id: 2,
          name: '一级 2',
          children: [
            {
              id: 5,
              name: '二级 2-1',
            },
            {
              id: 6,
              name: '二级 2-2',
            },
          ],
        },
        {
          id: 3,
          name: '一级 3',
          children: [
            {
              id: 7,
              name: '二级 3-1',
            },
            {
              id: 8,
              name: '二级 3-2',
            },
          ],
        },
      ],
    }
  },
}
</script>
<style scoped>
</style>